package com.example.partybuildingsoftware0123456789hwh1127;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpRetryException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class LoginActivity extends AppCompatActivity {

    private EditText edtTxtLoginUserName;
    private EditText edtTxtLoginPassword;
    private Button btnLoginLogin;
    private Button btnLoginReset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
        btnLoginLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName=edtTxtLoginUserName.getText().toString().trim();
                String password=edtTxtLoginPassword.getText().toString().trim();
                //在子线程中进行网络访问，获取账号数据
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            URL url = new URL(AppData.getServiceUrl()+"/json/account.json");
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            conn.setRequestMethod("GET");
                            conn.setConnectTimeout(5000);
                            InputStream is = conn.getInputStream();//字节流0101010100101010101001110101
//                           转换为字符流
                            InputStreamReader isr = new InputStreamReader(is);
                            BufferedReader br = new BufferedReader(isr);
                            String json="";
                            String temp;
                            while((temp=br.readLine())!=null){
                                json+=temp;
                                json+="\n";
                            }
                            Log.i("TAG", "run: "+json);
                            br.close();
                            isr.close();
                            is.close();
                            conn.disconnect();

                            JSONArray jsonArray = new JSONArray(json);
                            boolean loginState=false;
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject obj = jsonArray.getJSONObject(i);
                                String username1=obj.getString("userName");
                                String password1=obj.getString("password");
                                if(userName.equals(username1)&&password.equals(password1)){
                                    loginState=true;
                                    break;
                                }
                            }
                            final boolean finalLoginState = loginState;
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(finalLoginState){
                                        Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                                        //登录成功
                                        //跳转界面
                                        startActivity(new Intent(LoginActivity.this,PartyActivity.class));
                                    }else {
                                        Toast.makeText(LoginActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });


                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();



            }
        });
        btnLoginReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edtTxtLoginPassword.setText("");
                edtTxtLoginUserName.setText("");
            }
        });
    }

    private void initView() {
        edtTxtLoginUserName = (EditText) findViewById(R.id.edtTxtLoginUserName);
        edtTxtLoginPassword = (EditText) findViewById(R.id.edtTxtLoginPassword);
        btnLoginLogin = (Button) findViewById(R.id.btnLoginLogin);
        btnLoginReset = (Button) findViewById(R.id.btnLoginReset);
    }
}